#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "ourhdr.h"

int main(int argc, char** argv){
	pid_t pid;
	int fd, dup_fd;
	char buf[100];

	if((fd = open(argv[1], O_RDWR | O_CREAT)) < 0)
		err_sys("open error");
	if((dup_fd = dup(fd)) < 0)
		err_sys("dup error");
	strcpy(buf, "a message from dup_fd\n");
	write(dup_fd, buf, strlen(buf));
	if( (pid = fork()) < 0)
		err_sys("fork error");
	else if(pid == 0){
		strcpy(buf, "a message from child's dup_fd\n");
		write(dup_fd, buf, strlen(buf));
		exit(0);
	}
	else{
		wait(NULL);
		strcpy(buf, "a message from parent's fd\n");
		write(fd, buf, strlen(buf));
	}
	exit(0);
}
